package com.ruoyi.web.controller.files.utils;

import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @author dingling
 * @desc 视频处理工具
 * @date 2023-08-08 14:14
 **/

@Slf4j
public class VideoUtils {
    /*** 获取指定视频的帧并保存为图片
     * @param file  源视频文件地址
     * @param frameFile  截取帧的图片存放地址
     * @throws  Exception
     */
    public static void fetchPic(File file, String frameFile) throws Exception {
        FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file);
        ff.start();
        int lenght = ff.getLengthInFrames();
        File targetFile = new File(frameFile);
        int i = 0;
        Frame frame = null;
        while (i < lenght) {// 过滤前5帧，避免出现全黑的图片，依自己情况而定
            frame = ff.grabFrame();
            if ((i > 50) && (frame.image != null)) {
                break;
            }
            i++;
        }
        String imgSuffix = "jpg";
        if (frameFile.indexOf('.') != -1) {
            String[] arr = frameFile.split("\\.");
            if (arr.length >= 2) {
                imgSuffix = arr[arr.length - 1];
            }
        }
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage srcBi = converter.getBufferedImage(frame);
        int owidth = srcBi.getWidth();
        int oheight = srcBi.getHeight();// 对截取的帧进行等比例缩放
        int width = 800;
        int height = (int) (((double) width / owidth) * oheight);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        bi.getGraphics().drawImage(srcBi.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        try {
            ImageIO.write(bi, imgSuffix, targetFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ff.stop();
    }

    /*** 获取视频时长，单位为秒
     * @param file 即为视频地址
     * @return 时长（秒）
     **/
    public static Long getVideoTime(File file) {
        long times = 0L;
        try {
            FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file);
            ff.start();
            times = ff.getLengthInTime() / (1000 * 1000);
            ff.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }


    public static void testPic() {
        File file = new File("E:\\data\\7.视频-定胜糕.mp4");
        try {
            VideoUtils.fetchPic(file, "E:\\data\\7.视频-定胜糕.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testTime() {
        File file = new File("E:\\data\\7.视频-定胜糕.mp4");
        Long videoTime = VideoUtils.getVideoTime(file);
        System.out.println(videoTime);
    }

    public static void main(String[] args) throws Exception{
        testPic();
//        testTime();
//        PdfUtils.pdfGetCoverImage("E:\\data\\丁铃-java开发.pdf","E:\\data\\dl简历封面.jpg");
    }


}